layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
        elem: '#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem: '#endTime'
        ,value:new Date()
    });

    /**
     * 临时卡打卡班次表管理
     */
    var TransportRegistrationShiftSchedule = {
        tableId: "transportRegistrationShiftScheduleTable"
    };

    /**
     * 初始化表格的列
     */
    TransportRegistrationShiftSchedule.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'operateName', title: '项目名称'},
            {field: 'shiftScheduleName', title: '班次名称'},
            {field: 'workShiftTime', title: '上班时间'},
            {field: 'closeTime', title: '下班时间'},
            {field: 'laborServices', title: '劳务公司'},
            {field: 'temporaryName', title: '打卡人'},
            {field: 'idcard', title: '身份证'},
            {field: 'tel', title: '手机号码'},
            {field: 'startTime', title: '上班打卡时间'},
            {field: 'endTime', title: '下班打卡时间'},
            {field: 'allTimes', title: '总时长'},
            {field: 'remark', title: '备注'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    TransportRegistrationShiftSchedule.search = function () {
        var queryData = {};
        queryData['operateId'] = $("#operateId").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['temporaryName'] = $("#temporaryName").val();
        table.reload(TransportRegistrationShiftSchedule.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    TransportRegistrationShiftSchedule.openAddDlg = function () {
        window.location.href = Feng.ctxPath + '/transportRegistrationShiftSchedule/add';
    };

    /**
     * 导出excel按钮
     */
    TransportRegistrationShiftSchedule.exportExcel = function () {
        var checkRows = table.checkStatus(TransportRegistrationShiftSchedule.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['operateId'] = $("#operateId").val();
            queryData['startTime'] = $("#startTime").val();
            queryData['endTime'] = $("#endTime").val();
            queryData['temporaryName'] = $("#temporaryName").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/transportRegistrationShiftSchedule/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    TransportRegistrationShiftSchedule.openEditDlg = function (data) {
        window.location.href = Feng.ctxPath + '/transportRegistrationShiftSchedule/edit?id=' + data.id;
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TransportRegistrationShiftSchedule.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/transportRegistrationShiftSchedule/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(TransportRegistrationShiftSchedule.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TransportRegistrationShiftSchedule.tableId,
        url: Feng.ctxPath + '/transportRegistrationShiftSchedule/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: TransportRegistrationShiftSchedule.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TransportRegistrationShiftSchedule.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TransportRegistrationShiftSchedule.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TransportRegistrationShiftSchedule.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TransportRegistrationShiftSchedule.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TransportRegistrationShiftSchedule.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TransportRegistrationShiftSchedule.onDeleteItem(data);
        }
    });
});
